var DrawTool = {
	pointGraphicLayer: null,
	pointGraphicLayerGroup: null,
	polylineGraphicLayer: null,
	polygonGraphicLayer: null,
	pointArr: null,
	lastGeometry: null,
	lastpoint: null,
	handler: null,
	toolbartag: null,
	init: function() {
		DrawTool.pointArr = new Array();
	},

	endDraw: function() {
		//触发draw-end事件
		//GMIdentify.mapClickIdentify= map.on("tap", GMIdentify.executeIdentifyTask);
		var currentwv = plus.webview.currentWebview();
		mui.fire(currentwv, 'draw-end', {
			//           geometry: DrawTool.lastGeometry
		});
		//  清除数组
		DrawTool.pointArr.splice(0, DrawTool.pointArr.length);
		//  	 把drawGraphicLayerTemp去除
		MAP.removeLayer(DrawTool.drawGraphicLayerTemp);
		//  	 关闭infowindow
		//MAP.infoWindow.hide();
		//解除事件
		DrawTool.handler.remove();

	},
	activate: function(customtype, toolbartag) {
		//  清除数组
		DrawTool.pointArr.splice(0, DrawTool.pointArr.length);
		//GMIdentify.mapClickIdentify.remove();
		MAP.off('click');
		DrawTool.pointGraphicLayerGroup = new L.LayerGroup();
		DrawTool.handler = MAP.on("click", function(event) {
			// 	   获取点击的点
			if(DrawTool.polylineGraphicLayer) {
				MAP.removeLayer(DrawTool.polylineGraphicLayer);
			}
			if(DrawTool.polygonGraphicLayer) {
				MAP.removeLayer(DrawTool.polygonGraphicLayer);
			}
			var cPoint = new L.LatLng(event.lat,event.lng);
			DrawTool.pointArr.push(cPoint);
			// 	    记录最后点击的point 
			DrawTool.lastpoint = cPoint;
			var pointCount = DrawTool.pointArr.length;

			// 	  	根据点的个数画图
			var geometry1 = null;
			var geometry2 = null;
			if(pointCount == 1) {
				// var point = new esri.geometry.Point(cPoint.x, cPoint.y, cPoint.spatialReference);
				geometry1 = cPoint;
				// symbol1 = DrawTool.markerSymbol;
				//				return;
			} else {
				//对于gcj02坐标系要定义坐标，不然默认是4326
				DrawTool.polylineGraphicLayer = L.polyline(DrawTool.pointArr, {
					color: '#179efa'
				}).addTo(MAP);
				// setLatLngs
				// polyline.addPath(DrawTool.pointArr);
				geometry1 = DrawTool.polylineGraphicLayer;
				// symbol1 = DrawTool.lineSymbol;
				if(pointCount > 2) {
					//					var polygon = new esri.geometry.Polygon();
					//					polygon.addRing(DrawTool.pointArr);
					DrawTool.polygonGraphicLayer = L.polygon(DrawTool.pointArr, {
						color: '#179efa'
					});
					geometry2 = DrawTool.polygonGraphicLayer;
					//symbol2 = DrawTool.fillSymbol_gray;
				}
			}
			// DrawTool.drawGraphicLayerTemp.clear();
			DrawTool.pointGraphicLayer = L.circleMarker(cPoint, {
				radius: 3,
				weight: 0,
				fillColor: '#179efa',
				fillOpacity: 1
			});

			DrawTool.pointGraphicLayerGroup.addLayer(DrawTool.pointGraphicLayer).addTo(MAP);
			// 标绘出所有点
			//			if(pointCount > 0) {
			//				mui.each(DrawTool.pointArr, function(idx, obj) {
			//					DrawTool.pointGraphicLayer = L.circleMarker(obj, {
			//						radius: 3,
			//						weight: 0,
			//						fillColor: '#179efa',
			//						fillOpacity: 1
			//					}).addTo(MAP);
			//				})
			//			}

			//			if(geometry1 != null) {
			//				var graphic1 = new esri.Graphic(geometry1, symbol1);
			//				DrawTool.drawGraphicLayerTemp.add(graphic1);
			//				DrawTool.lastGeometry = geometry1;
			//			}
			if(geometry2 != null && customtype == "polygon") {
				DrawTool.polygonGraphicLayer.addTo(MAP);
			}

			// 	   	提示框 区域查询
			if(customtype == "polygon" && toolbartag == 'PolygonSearch' && pointCount > 2) { 
				//				 polygonSearch
				L.popup().setLatLng(DrawTool.lastpoint)
					.setContent('点击开始查询')
					.openOn(MAP);
				var infowindowhref_ele = document.getElementById("infowindowhref");
				infowindowhref_ele.addEventListener("tap", function() {
					DrawTool.endDraw();
				});
			} else if(customtype == "polygon" && toolbartag == 'AreaMeasure' && pointCount > 2) {
				DrawTool.outputArea(geometry2);
			} else if(customtype == "polyline" && toolbartag == 'DistanceMeasure' && pointCount >= 2) {
				DrawTool.outputDistance(geometry1);
			}
		});
	}, //投影转换完成后调用方法
	outputDistance: function(geometry) {
		// 计算距离
		var resultObj = DrawTool.calculateLength(geometry);
		L.popup({
				offset: L.point(0, 7)
			}).setLatLng(DrawTool.lastpoint)
			.setContent(resultObj.contentHTML)
			.openOn(MAP);

		//监听 popup的关闭事件
		//只能监听手动关闭事件
		mui.each(mui(".leaflet-popup-close-button"), function(idx, obj) {
			obj.addEventListener("tap", function() {
				DrawTool.clear();
			});
		})
	},

	////显示测量面积
	outputArea: function(geometry) {
		// 需调用leaflet 计算面积接口
		var area = turf.area(geometry.toGeoJSON()) / 1000
		var result = Math.abs(area);
		var resultObj = DrawTool.calculateArea(geometry);
		L.popup().setLatLng(DrawTool.lastpoint)
			.setContent(resultObj.contentHTML)
			.openOn(MAP);
		mui.each(mui(".leaflet-popup-close-button"), function(idx, obj) {
			obj.addEventListener("tap", function() {
				DrawTool.clear();
			});
		})
	},
	//计算测量距离
	calculateLength: function(geometry) {
		var lineLength = turf.length(geometry.toGeoJSON(), {
			units: 'meters'
		})
		var lineKmLength = parseInt(lineLength /1000);
		var contentHTML = lineLength < 1000 ? (parseInt(lineLength) + '米') : (lineKmLength.toFixed(1) + ' 公里');
		var resultObj = {
			contentHTML: contentHTML
		}
		return resultObj;

	},

	//计算测量面积
	calculateArea: function(geometry) {
		var areaSquareMeters = turf.area(geometry.toGeoJSON())
		var result = Math.abs(areaSquareMeters);
		var areaSquareKm = parseInt(result) / 1000000;
		areaNum = result < 1000000 ? (parseInt(result) + '平方米') : (areaSquareKm.toFixed(1) + ' 平方公里');
		var contentHTML = areaNum;
		var resultObj = {
			contentHTML: contentHTML
		}
		return resultObj;

	},
	clear: function() {
		//解除事件
		if(DrawTool.handler != null) {
			// DrawTool.handler.remove();
			// MAP.off(DrawTool.handler);
			MAP.off('click')
			// 添加identify 的click事件
			GMIdentify.beginIdentify()
		}
		MAP.closePopup()
		if(DrawTool.pointGraphicLayerGroup) {
			MAP.removeLayer(DrawTool.pointGraphicLayerGroup);
		}
		if(DrawTool.polylineGraphicLayer) {
			MAP.removeLayer(DrawTool.polylineGraphicLayer);
		}
		if(DrawTool.polygonGraphicLayer) {
			MAP.removeLayer(DrawTool.polygonGraphicLayer);
		}

	}
}